
#include "stdafx.h"
#include "CountTime.h"
namespace Mz_CommonUtility
{
	static void __gettimeofday( timeval *pTimeValue );

	CounterID startCount()
	{
		timeval tv_start;
		__gettimeofday( &tv_start );
		return tv_start;
	}

	UINT32 countTimeIn( const CounterID &counterID, TimeUnit unit )
	{
		timeval tv_end;
		__gettimeofday( &tv_end );

		switch( unit )
		{
		case MircoSec:
			return ( tv_end.tv_sec - counterID.tv_sec ) * 1000000 + tv_end.tv_usec - counterID.tv_usec;
		case MilliSec:
			return ( tv_end.tv_sec - counterID.tv_sec ) * 1000 + 
				( tv_end.tv_usec - counterID.tv_usec + 500 ) / 1000;
		case Second:
			return ( tv_end.tv_sec - counterID.tv_sec ) + 
				( tv_end.tv_usec - counterID.tv_usec + 500000) / 1000000;
		case Minute:
			return ( tv_end.tv_sec - counterID.tv_sec + 30 ) / 60 + 
				( tv_end.tv_usec - counterID.tv_usec + 30000000 ) / 60000000;
		default:
			return 0;
		}
	}


	void __gettimeofday( timeval *tvp )
	{
		if( !tvp )
		{
			return;
		}
		DWORD tc = GetTickCount();

		tvp->tv_sec = tc / 1000;

		tvp->tv_usec = ( tc % 1000 ) * 1000;

	}

}
